﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormQuanlytheloai : Form
    {
        private Cxulytheloai xuly;


        public FormQuanlytheloai()
        {
            InitializeComponent();
        }

        private void FormQuanlytheloai_Load(object sender, EventArgs e)
        {
            xuly = new Cxulytheloai();
            hienthidstheloai(xuly.getdsTheLoai());

        }

        //hien thi danh sach the loai len gridview
        public void hienthidstheloai(List<TheLoai> ds)
        {
            foreach (TheLoai tl in ds)
            {
                if (tl.TheLoaiStatus == "live")
                {
                    int i = dgvTheloai.Rows.Add();
                    dgvTheloai.Rows[i].Cells[0].Value = tl.TheLoaiNo;
                    dgvTheloai.Rows[i].Cells[1].Value = tl.TheLoaiName;
                    if (tl.TheLoaiDescription == null)
                    {
                        dgvTheloai.Rows[i].Cells[2].Value = "";
                    }
                    else
                        dgvTheloai.Rows[i].Cells[2].Value = tl.TheLoaiDescription;
                }
            }
        }

        //su kien xay ra khi nguoi dung dang delete 1 row
        private void dgvTheloai_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            string ten = e.Row.Cells[1].Value.ToString();
            DialogResult response = MessageBox.Show("Bạn có chắc muốn xóa?", "Xóa thể loại?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if ((response == DialogResult.No))
            {
                e.Cancel = true;
            }
            else
            {
                xuly.XoaTheloai(ten);
                e.Cancel = false;
            }
        }

        //su kien xay ra khi nguoi dung click ra khoi 1 row 
        private void dgvTheloai_RowValidated(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex != dgvTheloai.RowCount - 1)
            {
                string ma =Convert.ToString(dgvTheloai.Rows[e.RowIndex].Cells["Theloaino"].Value);
                if (kiemtratontaitheloai(ma) == false) // neu ma nay chua ton tai 
                {
                    #region Thêm mới
                    TheLoai tl = new TheLoai();
                    tl.TheLoaiID = xuly.getdsTheLoai().Last().TheLoaiID + 1;
                    tl.TheLoaiNo = "TL"+tl.TheLoaiID.ToString();
                    tl.TheLoaiName = dgvTheloai.Rows[e.RowIndex].Cells["Theloaiten"].Value.ToString();
                    tl.TheLoaiDescription = Convert.ToString(dgvTheloai.Rows[e.RowIndex].Cells["TheloaiDes"].Value);
                    tl.TheLoaiStatus = "live";
                    xuly.ThemTheloai(tl);
                    dgvTheloai.Rows[e.RowIndex].Cells["Theloaino"].Value = tl.TheLoaiNo;
                    #endregion
                }
                else if (kiemtratontaitheloai(ma) == true)
                {
                    if (kiemtrathaydoirow(dgvTheloai.Rows[e.RowIndex]) == true)
                    {
                        TheLoai tl = new TheLoai();
                        tl.TheLoaiID = xuly.TimTheloaitheoma(dgvTheloai.Rows[e.RowIndex].Cells["Theloaino"].Value.ToString()).TheLoaiID;
                        tl.TheLoaiNo = ma;
                        tl.TheLoaiName = dgvTheloai.Rows[e.RowIndex].Cells["Theloaiten"].Value.ToString();
                        tl.TheLoaiDescription =Convert.ToString(dgvTheloai.Rows[e.RowIndex].Cells["TheloaiDes"].Value);
                        xuly.SuaTheloai(tl);

                    }
                }
            }

        }

        //kiem tra ton tai cua mot the loai
        public bool kiemtratontaitheloai(string ma)
        {
            bool flag = false;
            foreach (TheLoai tl in xuly.getdsTheLoai())
            {
                if (tl.TheLoaiNo.Equals(ma))
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        //kiem tra row co thay doi gia tri hay khong
        public bool kiemtrathaydoirow(DataGridViewRow r)
        {
            bool flag = false;
            foreach (DataGridViewCell c in r.Cells)
            {
                if (c.EditedFormattedValue != c.Value)
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }










    }
}
